package com.example.haoqisystem.Utils;



import com.example.haoqisystem.response.PageResult;

import java.util.List;

public class PageUtil<T> {
    public static <T> PageResult<T> getPage(List<T> allData, int pageNumber, int pageSize) {
        int totalElements = allData.size();
        int totalPages = (int) Math.ceil((double) totalElements / pageSize);

        int fromIndex = (pageNumber - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, totalElements);
        if (fromIndex > toIndex) {
            return new PageResult<>(null, pageNumber, pageSize, totalPages, totalElements);
        }
        List<T> content = allData.subList(fromIndex, toIndex);

        return new PageResult<>(content, pageNumber, pageSize, totalPages, totalElements);
    }
}

